|
|
|
|
|
|
|
semantics, each of such classes shares an is a relationship with the base class. For instance, a checking account is a type of bank account. Notice the use of the word type. A class's interface describes its type. For example, when you say CheckingAccount is a type of BankAccount, you are saying that the former inherits the interface of BankAccount. Therefore, if BankAccount has a public method deposit, CheckingAccount implements that same method in its own unique way, if necessary. In Visual Basic, this is a relationship would be realized as follows: |
|
|
|
|
|
|
|
|
Class: CheckingAccount
Implements BankAccount 'In the General Declarations section of Checking
'Account
Private Sub BankAccount_deposit()
'Insert code here
End Sub
Class: BankAccount
Public Sub deposit()
End Sub |
|
|
|
|
|
|
|
|
To get a better feel for inheritance in these classes, let's create them in Visual Basic. If you don't have it up and running, start it at this time with a new Standard project. Choose Add-Ins | Class Builder Utility from the menu. Simply create these classes by choosing File | New | Class from the menu for each. For the BankAccount class, be sure to insert a public function, deposit. To add a new method, select the BankAccount class in the Classes: browser, and select File | New | Method. You should see the Method Builder dialog box. Type in deposit and click the + button to add an argument, argAmountAsSingle. Click OK. Choose File | Update | Project and then File | Exit from the Class Builder menu. You now have two new class modules. Make the class module CheckingAccount the current module by clicking on it. Go to the General Declarations section, and type in the following: |
|
|
|
|
|
|
|
|
Check what you've typed in with Figure 4.1. The Implements keyword is how you tell Visual Basic that you want to inherit the interface of another class. Now click the (General) list box of the class module. You should see BankAccount listed just above the Class item. Choose BankAccount now. You should now see a PrivateSubBankAccount_deposit (see Figure 4.2). You can enter any code you like. When you eventually run the project, all calls to the deposit method go to the implementation class (CheckingAccount, for this example). |
|
|
|
|
|